package com.yuzhenxin.coursesystem.listener;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.yuzhenxin.coursesystem.entity.CourseDO;
import com.yuzhenxin.coursesystem.service.CourseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
public class CourseListener extends AnalysisEventListener<CourseDO> {

    private final CourseService courseService;

    private final List<CourseDO> courseEntityList = new ArrayList<>();

    @Override
    public void invoke(CourseDO courseEntity, AnalysisContext analysisContext) {
        courseEntityList.add(courseEntity);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (ObjectUtil.isNotEmpty(courseEntityList)) {
            courseService.saveBatch(courseEntityList);
            log.info("保存数据成功");
        }
    }
}
